package gdou.gym.web;

import gdou.gym.exception.SysException;
import gdou.gym.token.TokenIgnore;
import org.springframework.web.bind.annotation.*;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.concurrent.ConcurrentHashMap;

import static gdou.gym.exception.StateCode.LOGIN_ERROR;


@RestController
@RequestMapping("/all")
@CrossOrigin
@WebListener
public class MySessionListen implements HttpSessionListener {
    private static final ConcurrentHashMap<String, Object> M1 = new ConcurrentHashMap<>();
    private static final Object V = new Object();

    @GetMapping("/des")
    public void destroy(@RequestHeader String token) {
        M1.remove(token);
    }

    public static void put(String token) {
        System.out.println(M1);
        if (M1.containsKey(token)) {
            throw new SysException(LOGIN_ERROR);

        }else {
            M1.put(token, V);
        }


    }

    @TokenIgnore
    @GetMapping("/tt")
    public void test(HttpServletRequest request, HttpSession session) {
        System.out.println(request.getSession());
        System.out.println(session);
    }

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        System.out.println("sessionCreated");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        System.out.println("sessionDestroyed");
    }
}

